<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>vue</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
  </head>

  <body>
    <div id="app">
      <parent_com></parent_com>
    </div>

    <template id="parent">
      <div>
        <h3>父组件</h3>
        <p>p_name:{{p_name}}</p>
        <son_com :name="p_name"></son_com>
      </div>
    </template>

    <template id="son">
      <div>
        <h3>子组件</h3>
        <button @click="changeName">设置</button>
        <p>s_name:{{name}}</p>
      </div>
    </template>

    <script>
      let son_com = {
        template: "#son",
        props: ["name"],
        data() {
          return {};
        },
        methods: {
          changeName() {
            this.$parent.setName();
          },
        },
      };

      let parent_com = {
        template: "#parent",
        data() {
          return {
            p_name: "小王",
          };
        },
        methods: {
          setName() {
            this.p_name = "小明";
          },
        },
        components: {
          son_com,
        },
      };

      let vm = new Vue({
        el: "#app",
        data: {},
        methods: {},
        components: {
          parent_com,
        },
      });
    </script>
  </body>
</html>
